-----------------------------------------------------------------------------------------------------------------------------------------------------------
      name:  plog_1
       log:  /Users/michael/Documents/git_repos/maptile_geo_templates/build/build_state.log
  log type:  text
 opened on:   3 Jan 2017, 12:28:25

. *! 3jan2017  Michael Stepner, stepner@mit.edu
. 
. * imports 2010 State shapefile into Stata format
. 
. 
. /*******************************
> 
> ** INPUT FILES ** 
> - gz_2010_us_040_00_20m.zip
>         Provided by U.S. Census Bureau at http://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
> - state_fips_abbrev.dta
>         Contains a mapping between State FIPS codes and State standardized 2-letter abbreviations
> 
> *******************************/
. 
. 
. *** Step 0: Initialize
. 
. * Check if run using -project-
. return clear

. capture project, doinfo

. if (_rc==0 & !mi(r(pname))) global root `r(pdir)'  // run using -project-

. else {  // running directly
. 
.         global root "/Users/michael/Documents/git_repos/maptile_geo_templates/build"
. 
.         * Disable project (since running do-files directly)
.         cap program drop project
.         program define project
.                 di "Project is disabled, skipping project command. (To re-enable, run -{stata program drop project}-)"
.         end
.         
. }

. 
. * Specify subdirectories
. global raw "$root/raw_data/state"

. global out "$root/geo_templates/state"

. global test "$root/tests/state"

. 
. * Add utility programs to path
. adopath ++ "$root/util"
  [1]              "/Users/michael/Documents/git_repos/maptile_geo_templates/build/util"
  [2]  (UPDATES)   "/Applications/Stata/ado/updates/"
  [3]  (BASE)      "/Applications/Stata/ado/base/"
  [4]  (SITE)      "/Applications/Stata/ado/site/"
  [5]              "."
  [6]  (PERSONAL)  "~/Library/Application Support/Stata/ado/personal/"
  [7]  (PLUS)      "~/Library/Application Support/Stata/ado/plus/"
  [8]  (OLDPLACE)  "~/ado/"

. 
. * Tell -project- that we use -save12-
. project, original("$root/util/save12.ado")
project build_state > do-file uses original: "util/save12.ado" filesig(2718885224:622)

. 
. 
. *** Step 1: Unzip & convert shape file to dta
. 
. project, original("$raw/gz_2010_us_040_00_20m.zip")
project build_state > do-file uses original: "raw_data/state/gz_2010_us_040_00_20m.zip" filesig(905514951:648959)

. cd "$raw"
/Users/michael/Documents/git_repos/maptile_geo_templates/build/raw_data/state

. unzipfile "$raw/gz_2010_us_040_00_20m.zip", replace
    inflating: gz_2010_us_040_00_20m.dbf
    inflating: gz_2010_us_040_00_20m.prj
    inflating: gz_2010_us_040_00_20m.shp
    inflating: gz_2010_us_040_00_20m.shx
    inflating: gz_2010_us_040_00_20m.xml
successfully unzipped /Users/michael/Documents/git_repos/maptile_geo_templates/build/raw_data/state/gz_2010_us_040_00_20m.zip to current directory

. 
. shp2dta using "$raw/gz_2010_us_040_00_20m", database("$out/state_database_temp") ///
>         coordinates("$out/state_coords_temp") genid(_polygonid) replace
type: 5

. 
. 
. *** Step 2: Clean database
. 
. use "$out/state_database_temp", clear

. rename STATE statefips

. rename NAME statename

. keep statefips statename _polygonid

. destring statefips, replace
statefips has all characters numeric; replaced as byte

. project, original("$raw/state_fips_abbrev.dta") preserve
project build_state > do-file uses original: "raw_data/state/state_fips_abbrev.dta" filesig(3638656187:775)

. merge 1:1 statefips using "$raw/state_fips_abbrev.dta", assert(2 3) keep(3) nogen

    Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                                52  
    -----------------------------------------

. drop if statefips>56
(1 observation deleted)

. save12 "$out/state_database_clean.dta", replace
file /Users/michael/Documents/git_repos/maptile_geo_templates/build/geo_templates/state/state_database_clean.dta saved

. project, creates("$out/state_database_clean.dta")
project build_state > do-file creates: "geo_templates/state/state_database_clean.dta" filesig(2552821413:2232)

. 
. *** Step 3: Clean coordinates
. use "$out/state_coords_temp.dta", clear

. gen _polygonid=_ID

. merge m:1 _polygonid using "$out/state_database_clean.dta", assert(1 3) keep(3) nogen

    Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                            51,372  
    -----------------------------------------

. 
. ** Reshape U.S.
. project, original("$root/util/reshape_us.do") preserve
project build_state > do-file uses original: "util/reshape_us.do" filesig(658224549:2054)

. do "$root/util/reshape_us.do"

. *! 31jan2014, Michael Stepner, stepner@mit.edu
. * reshape_us.do: takes a coordinates file from the U.S. Census Bureau and rescales CONUS, AK, HI; shifts AK HI below CONUS.
. * Note: For this file to work properly the shapefile needs to be in WGS1984 coordinate system.
. 
. 
. 
. ** Drop Alaska islands off to the far right
. *sum _X if statefips==2
. *sum _X if statefips==2 & inrange(_X,-50,50)
. drop if statefips==2 & _X>0 & !missing(_X)
(341 observations deleted)

. 
. ** Drop leftmost islands of Hawaii
. *sum _X if statefips==15
. *sum _X if statefips==15 & inrange(_X,-160.45,-160.4)
. drop if statefips==15 & _X<-160.4 & !missing(_X)
(7 observations deleted)

. 
. ** Rescale U.S. to a better projection: by default, stretched too wide
. *sum _Y if !inlist(statefips,2,15)
. replace _Y=_Y*1.355 if !inlist(statefips,2,15)
(39992 real changes made)

. 
. ** Rescale Alaska to a better projection: by default, stretched FAR too wide
. replace _Y=_Y*2.39 if statefips==2
(10025 real changes made)

. 
. ** Rescale Hawaii to a better projection: by default, stretched a touch too wide
. replace _Y=_Y*1.056 if statefips==15
(766 real changes made)

. 
. 
. ** Move Alaska to convenient location below continental U.S.
. 
. * Scale AK down
. replace _X=_X/(16/3) if statefips==2
(10025 real changes made)

. replace _Y=_Y/(16/3) if statefips==2
(10025 real changes made)

. 
. * Move AK to left hand side of CONUS
. sum _X if !inlist(statefips,2,15) /* leftmost is -124.7332 */

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
          _X |     39992   -89.83356     14.1331  -124.7332  -66.94989

. local xshift=r(min)

. sum _X if statefips==2

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
          _X |     10025   -28.59176    2.482003  -33.59013  -24.37126

. local xshift=`xshift'-r(min)

. 
. replace _X=_X+`xshift'+1 if statefips==2
(10025 real changes made)

. 
. * Move AK south of CONUS
. sum _Y if !inlist(statefips,2,15) /* lowest is 33.25807 */

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
          _Y |     39992     53.4685    7.810842   33.25807   66.91581

. local yshift=r(min)

. sum _Y if statefips==2

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
          _Y |     10025    26.37665    2.015733    22.9529   31.97487

. local yshift=`yshift'-r(min)

. replace _Y=_Y+`yshift'-1 if statefips==2
(10025 real changes made)

. 
. 
. ** Move Hawaii to convenient location below continental U.S.
. 
. * Scale HI up
. replace _X=_X*1.2 if statefips==15
(766 real changes made)

. replace _Y=_Y*1.2 if statefips==15
(766 real changes made)

. 
. * Move HI right of AK
. sum _X if statefips==2

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
          _X |     10025   -118.7348    2.482003  -123.7332  -114.5143

. local xshift=r(max)

. sum _X if statefips==15

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
          _X |       766    -188.418    1.727941  -192.2995  -185.7713

. local xshift=`xshift'-r(min)

. 
. replace _X=_X+`xshift'+2.5 if statefips==15
(766 real changes made)

. 
. * Move HI south of CONUS
. sum _Y if !inlist(statefips,2,15) /* lowest is 33.25807 */

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
          _Y |     39992     53.4685    7.810842   33.25807   66.91581

. local yshift=r(min)

. sum _Y if statefips==15

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
          _Y |       766    26.32612    1.136674   23.97221   28.17387

. local yshift=`yshift'-r(min)

. 
. replace _Y=_Y+`yshift'+1 if statefips==15
(766 real changes made)

. 
end of do-file

. 
. ** Save coords dataset
. keep _ID _X _Y

. sort _ID, stable

. save12 "$out/state_coords_clean.dta", replace
file /Users/michael/Documents/git_repos/maptile_geo_templates/build/geo_templates/state/state_coords_clean.dta saved

. project, creates("$out/state_coords_clean.dta")
project build_state > do-file creates: "geo_templates/state/state_coords_clean.dta" filesig(148726545:868121)

. 
. *** Step 4: Clean up extra files
. erase "$out/state_database_temp.dta"

. erase "$out/state_coords_temp.dta"

. 
. *** Step 5: Reference other files using -project-
. project, relies_on("$root/readme.txt")
project build_state > do-file relies on: "readme.txt" filesig(2581144236:573)

. project, relies_on("$out/state_maptile.ado")
project build_state > do-file relies on: "geo_templates/state/state_maptile.ado" filesig(1114271236:2441)

. project, relies_on("$out/state_maptile.md")
project build_state > do-file relies on: "geo_templates/state/state_maptile.md" filesig(508556979:1053)

. project, relies_on("$out/state_maptile.smcl")
project build_state > do-file relies on: "geo_templates/state/state_maptile.smcl" filesig(501369264:1199)

. 
. *** Step 6: Test geo-specific options
. use "$out/state_database_clean.dta", clear

. rename _polygonid test

. 
. maptile test, geo(state) geofolder($out) ///
>         savegraph("$test/state_noopt.png") resolution(0.25) replace
(file /Users/michael/Documents/git_repos/maptile_geo_templates/build/tests/state/state_noopt.png written in PNG format)

. project, creates("$test/state_noopt.png") preserve
project build_state > do-file creates: "tests/state/state_noopt.png" filesig(975015736:229231)

. 
. foreach geoid in state statefips statename {
  2. 
.         maptile test, geo(state) geofolder($out) ///
>                 geoid(`geoid') ///
>                 savegraph("$test/state_geoid_`geoid'.png") resolution(0.25) replace
  3.         project, creates("$test/state_geoid_`geoid'.png") preserve
  4. 
. }
(file /Users/michael/Documents/git_repos/maptile_geo_templates/build/tests/state/state_geoid_state.png written in PNG format)
project build_state > do-file creates: "tests/state/state_geoid_state.png" filesig(975015736:229231)
(file /Users/michael/Documents/git_repos/maptile_geo_templates/build/tests/state/state_geoid_statefips.png written in PNG format)
project build_state > do-file creates: "tests/state/state_geoid_statefips.png" filesig(975015736:229231)
(file /Users/michael/Documents/git_repos/maptile_geo_templates/build/tests/state/state_geoid_statename.png written in PNG format)
project build_state > do-file creates: "tests/state/state_geoid_statename.png" filesig(975015736:229231)

. 
end of do-file
      name:  plog_1
       log:  /Users/michael/Documents/git_repos/maptile_geo_templates/build/build_state.log
  log type:  text
 closed on:   3 Jan 2017, 12:28:48
-----------------------------------------------------------------------------------------------------------------------------------------------------------
